<template>
	<!-- #ifdef MP-WEIXIN -->
	<view>
		<view>
			<view class="content">
				<image class="logo" src="/static/bbdc.png"></image>
				<view class="text-area">
					<text class="title">邦邦打车</text>
				</view>
			</view>
			<!-- 有授权窗口 -->
			<button type="primary" class='btn' v-if="button==0" @click="getUserProfile">授权获取用户信息</button>
		</view>
	</view>
	<!-- #endif -->
</template>
<script>
	export default {
		// 获取code发送到后端获取openid
		onLoad() {
			uni.login({
				provider: 'zh_CN',
				success: function(res) {
					// console.log(res, 24);
					let code = res.code
					// console.log(code, 26)
					uni.request({
						url: 'http://localhost:8000/getOpenId',
						method: 'GET',
						data: {
							code: code
						},
						success: function(res) {
							console.log(res.data, 33);
							uni.setStorageSync("openid", res.data)
						}
					});
				},
			})
		},
		data() {
			return {
				button: 0
			}
		},
		methods: {
			//授权登录弹窗
			getUserProfile() {
				uni.showLoading({
					title: '加载中'
				});
				this.button = 1
				uni.getUserProfile({
					desc: '授权登录',
					success: (res) => {
						this.button = 0
						// 用户信息
						let userInfo = res.userInfo
						let openid = uni.getStorageSync('openid')
						// console.log(res);
						// console.log(userInfo.avatarUrl,userInfo.nickName)
						uni.request({
							url: 'http://localhost:8000/getUserOpenId',
							method: 'GET',
							data: {
								openid: openid,
							},
							success: function(res) {
								console.log(res.data, 64);
								if (res.data.result == 0) {
									uni.showModal({
										title: '是否绑定手机号',
										content: '',
										placeholderText: '请输入手机号',
										editable: true,
										confirm: false,
										success: function(res) {
											// console.log(res, 72)
											if (res.confirm) {
												uni.showLoading({
													title: '加载中'
												});
												let userTel = res.content
												let reg =
													/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0|1|2|3|5|6|7|8|9])\d{8}$/
												if (userTel == '') {
													uni.hideLoading();
													uni.showToast({
														title: '手机号不能为空',
														icon: 'error',
														duration: 2000
													});
													console.log('手机号不能为空')
												} else if (!reg.test(userTel)) {
													uni.hideLoading();
													uni.showToast({
														title: '手机号输入格式有误',
														icon: 'error',
														duration: 2000
													});
													// console.log('手机号输入格式有误')
												} else {
													uni.hideLoading();
													uni.request({
														url: 'http://localhost:8000/userTable',
														data: {
															userTel: userTel,
															openid: openid,
															avatarUrl: userInfo
																.avatarUrl,
															userInfo: userInfo
																.nickName,
														},
														success: function(
															res) {
															console.log(res
																.data,
																95);
															if (res.data
																.result ==
																0) {
																uni.showToast({
																	title: '手机号已被注册',
																	icon: 'error',
																	duration: 2000
																});
															} else {
																let userTable = {
																	userId: res
																		.data
																		.data[
																			0
																		]
																		.userId,
																	userTel: res
																		.data
																		.data[
																			0
																		]
																		.phoneNum,
																	headPortrait: res
																		.data
																		.data[
																			0
																		]
																		.headPortrait,
																	credit: res
																		.data
																		.data[
																			0
																		]
																		.credit,
																	name: res
																		.data
																		.data[
																			0
																		]
																		.name,
																	wxName: res
																		.data
																		.data[
																			0
																		]
																		.wxName,
																}
																uni.setStorageSync(
																	"userTable",
																	userTable
																)
																uni.setStorageSync(
																	"openid",
																)
																uni.showLoading({
																	title: '正在跳转……'
																});
																setTimeout(
																	function() {
																		uni
																			.hideLoading();
																		// 连接放在这
																		uni.switchTab({
																			url: "/pages/home/home"
																		})
																	},
																	1000
																);
															}
														}
													});
												}
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
								} else {
									uni.request({
										url: 'http://localhost:8000/userTel',
										data: {
											openid: openid,
										},
										success: function(res) {
											console.log(res.data.data[0], 111);
											let userTable = {
												userId: res.data.data[0].userId,
												userTel: res.data.data[0].phoneNum,
												headPortrait: res.data.data[0]
													.headPortrait,
												credit: res.data.data[0].credit,
												name: res.data.data[0].name,
												wxName: res.data.data[0].wxName,
											}
											uni.setStorageSync("userTable", userTable)
											uni.setStorageSync("openid", )
										}
									});
									uni.showToast({
										title: '登录成功',
										duration: 2000
									});
									uni.showLoading({
										title: '正在跳转……'
									});
									setTimeout(function() {
										uni.hideLoading();
										uni.switchTab({
											url: "/pages/home/home"
										})
									}, 1000);
									console.log("跳转到打车页面")
								}
							}
						});

					},
					fail: (res) => {
						this.button = 0
						console.log(res)
					}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.btn {
		width: 90%;
		padding: 0 100rpx;
		margin-top: 100rpx;
	}
</style>
